package startup

import (
	"github.com/go-kratos/kratos/v2/log"
	"github.com/tx7do/kratos-transport/broker"
	"github.com/tx7do/kratos-transport/transport/rabbitmq"
	"gotham-plan-manage/internal/app/consumer"
	"gotham-plan-manage/internal/conf"
)

/*
   @author:zhongyang
   @date:2023/7/31
   @description:注册Rabbitmq
*/

func NewRabbitMQServer(conf *conf.Server, planManageConsumer *consumer.PlanManageConsumer) *rabbitmq.Server {
	server := rabbitmq.NewServer(
		rabbitmq.WithAddress([]string{conf.RabbitMQ.Url}),
		rabbitmq.WithExchange("gotham-common:handle-daily-user", true),
		rabbitmq.WithCodec("json"),
	)
	err := server.Connect()
	if err != nil {
		log.Fatal(err)
	}

	// 消费消息，执行每个用户的动作
	_, err = server.Subscribe("gotham-common:handle-daily-user", planManageConsumer.HandleUserDaily, nil, broker.WithQueueName("gotham-common:handle-daily-user:queue"))
	if err != nil {
		log.Fatal(err)
	}
	// 消费消息，执行每个用户的动作（延时版本）
	_, err = server.Subscribe("gotham-common:handle-daily-user:delay", planManageConsumer.HandleUserDaily, nil, broker.WithQueueName("gotham-common:handle-daily-user:queue"))
	if err != nil {
		log.Fatal(err)
	}
	return server
}
